package com.geostar.constructionland.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.geostar.constructionland.index.entity.*;
import com.geostar.constructionland.index.entity.model.IndexCancelModel;
import com.geostar.constructionland.index.mapper.DeleteInxOutStorageAcotMapper;
import com.geostar.constructionland.index.mapper.IndexCancelMapper;
import com.geostar.constructionland.index.mapper.InxOutStorageAcotMapper;
import com.geostar.constructionland.index.mapper.OaReceiptSheetMapper;
import com.geostar.constructionland.index.service.IndexCancelService;
import com.geostar.constructionland.index.util.CommonUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指标作废
 */
@Service
public class IndexCancelSerImpl implements IndexCancelService {
    @Resource
    InxOutStorageAcotMapper inxOutStorageAcotMapper;
    @Resource
    DeleteInxOutStorageAcotMapper deleteInxOutStorageAcotMapper;
    @Resource
    IndexCancelMapper indexCancelMapper;
    @Resource
    OaReceiptSheetMapper oaReceiptSheetMapper;

    /**
     * 作废指标
     * @param indexCancelModel
     */
    @Override
    @Transactional
    public void cancelIndex(IndexCancelModel indexCancelModel) {
        Map<String, Object> columnMap = new HashMap<>();
        String businessId = indexCancelModel.getBusinessId();
        columnMap.put("ywid", businessId);
        List<InxOutStorageAcotEntity> inxOutStorageAcotList = inxOutStorageAcotMapper.selectByMap(columnMap);
        Date nowDate = new Date();
        for (InxOutStorageAcotEntity inxOutStorageAcot : inxOutStorageAcotList) {
            // 新增指标出库台账删除记录
            DeleteInxOutStorageAcotEntity deleteInxOutStorageAcot = new DeleteInxOutStorageAcotEntity();
            deleteInxOutStorageAcot.setId(inxOutStorageAcot.getId());
            deleteInxOutStorageAcot.setInxStorageIAcotId(inxOutStorageAcot.getInxStorageIAcotId());
            deleteInxOutStorageAcot.setBuildLandAdd(inxOutStorageAcot.getBuildLandAdd());
            deleteInxOutStorageAcot.setAgclLand(inxOutStorageAcot.getAgclLand());
            deleteInxOutStorageAcot.setFarmland(inxOutStorageAcot.getFarmland());
            deleteInxOutStorageAcot.setOutStorageStatus(inxOutStorageAcot.getOutStorageStatus());
            deleteInxOutStorageAcot.setOperatorId(inxOutStorageAcot.getOperatorId());
            deleteInxOutStorageAcot.setOperatorTime(inxOutStorageAcot.getOperatorTime());
            deleteInxOutStorageAcot.setBusinessId(inxOutStorageAcot.getBusinessId());
            deleteInxOutStorageAcot.setRemark(inxOutStorageAcot.getRemark());
            deleteInxOutStorageAcot.setOutStorageType(inxOutStorageAcot.getOutStorageType());
            deleteInxOutStorageAcot.setOutStorageTime(inxOutStorageAcot.getOutStorageTime());
            deleteInxOutStorageAcot.setRemoveTime(nowDate);
            deleteInxOutStorageAcotMapper.insert(deleteInxOutStorageAcot);
            // 新增指标作废记录
            IndexCancelEntity indexCancel = new IndexCancelEntity();
            indexCancel.setId(CommonUtils.getUUID());
            indexCancel.setInxStorageIAcotId(inxOutStorageAcot.getInxStorageIAcotId());
            indexCancel.setBuildLandAdd(inxOutStorageAcot.getBuildLandAdd());
            indexCancel.setAgclLand(inxOutStorageAcot.getAgclLand());
            indexCancel.setFarmland(inxOutStorageAcot.getFarmland());
            indexCancel.setOperatorId(indexCancelModel.getOperatorId());
            indexCancel.setOperatorTime(nowDate);
            indexCancel.setBusinessId(businessId);
            indexCancel.setCancelReason(indexCancelModel.getCancelReason());
            indexCancelMapper.insert(indexCancel);
        }
        // 删除出库指标
        inxOutStorageAcotMapper.deleteByMap(columnMap);
        // 更新收件单操作状态
        UpdateWrapper<OaReceiptSheetEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ywid", businessId);
        OaReceiptSheetEntity oaReceiptSheetEntity = new OaReceiptSheetEntity();
        oaReceiptSheetEntity.setOperateState("0");
        oaReceiptSheetMapper.update(oaReceiptSheetEntity, updateWrapper);
    }
}
